home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / board / Crafty-15.19.lha / crafty-15.19 / src / learn.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  48KB  |  1,176 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <string.h>
  6. #include "chess.h"
  7. #include "data.h"
  8. #if defined(UNIX)
  9. #  include <unistd.h>
  10. #endif
  11.  
  12. /* last modified 03/11/98 */
  13. /*
  14. ********************************************************************************
  15. *                                                                              *
  16. *   LearnBook() is used to accumulate the evaluations for the first N moves    *
  17. *   out of book.  after these moves have been played, the evaluations are then *
  18. *   used to decide whether the last book move played was a reasonable choice   *
  19. *   or not.  (N is set by the #define LEARN_INTERVAL definition.)              *
  20. *                                                                              *
  21. *   there are three cases to be handled.  (1) if the evaluation is bad right   *
  22. *   out of book, or it drops enough to be considered a bad line, then the book *
  23. *   move will have its "learn" value reduced to discourage playing this move   *
  24. *   again.  (2) if the evaluation is even after N moves, then the learn        *
  25. *   value will be increased, but by a relatively modest amount, so that a few  *
  26. *   even results will offset one bad result.  (3) if the evaluation is very    *
  27. *   good after N moves, the learn value will be increased by a large amount    *
  28. *   so that this move will be favored the next time the game is played.        *
  29. *                                                                              *
  30. ********************************************************************************
  31. */
  32.  
  33. void LearnBook(TREE *tree, int wtm, int search_value, int search_depth, int lv,
  34.                int force) {
  35. /*
  36.  ----------------------------------------------------------
  37. |                                                          |
  38. |   if we have not been "out of book" for N moves, all     |
  39. |   we need to do is take the search evaluation for the    |
  40. |   search just completed and tuck it away in the book     |
  41. |   learning array (book_learn_eval[]) for use later.      |
  42. |                                                          |
  43.  ----------------------------------------------------------
  44. */
  45.   int t_wtm=wtm;
  46.   if (!book_file) return;
  47.   if (!(learning&book_learning)) return;
  48.   if (moves_out_of_book <= LEARN_INTERVAL && !force) {
  49.     if (moves_out_of_book) {
  50.       book_learn_eval[moves_out_of_book-1]=search_value;
  51.       book_learn_depth[moves_out_of_book-1]=search_depth;
  52.     }
  53.   }
  54. /*
  55.  ----------------------------------------------------------
  56. |                                                          |
  57. |   check the evaluations we've seen so far.  if they are  |
  58. |   within reason (+/- 1/3 of a pawn or so) we simply keep |
  59. |   playing and leave the book alone.  if the eval is much |
  60. |   better or worse, we need to update the learning count. |
  61. |                                                          |
  62.  ----------------------------------------------------------
  63. */
  64.   else if (moves_out_of_book == LEARN_INTERVAL+1 || force) {
  65.     int move, i, j, learn_value, read;
  66.     int secs, interval, last_book_move=-1;
  67.     float temp_value;
  68.     char cmd[32], buff[80], *nextc;
  69.     int best_eval=-999999, best_eval_p=0;
  70.     int worst_eval=999999, worst_eval_p=0, save_wtm;
  71.     int best_after_worst_eval=-999999, worst_after_best_eval=999999;
  72.     struct tm *timestruct;
  73.     int n_book_moves[512];
  74.     float book_learn[512], t_learn_value;
  75.  
  76.     if (moves_out_of_book < 1) return;
  77.     Print(128,"LearnBook() executed\n");
  78.     learning&=~book_learning;
  79.     interval=Min(LEARN_INTERVAL,moves_out_of_book);
  80.     if (interval < 2)  return;
  81.  
  82.     for (i=0;i<interval;i++) {
  83.       if (book_learn_eval[i] > best_eval) {
  84.         best_eval=book_learn_eval[i];
  85.         best_eval_p=i;
  86.       }
  87.       if (book_learn_eval[i] < worst_eval) {
  88.         worst_eval=book_learn_eval[i];
  89.         worst_eval_p=i;
  90.       }
  91.     }
  92.     if (best_eval_p < interval-1) {
  93.       for (i=best_eval_p;i<interval;i++)
  94.         if (book_learn_eval[i] < worst_after_best_eval)
  95.           worst_after_best_eval=book_learn_eval[i];
  96.     }
  97.     else worst_after_best_eval=book_learn_eval[interval-1];
  98.  
  99.     if (worst_eval_p < interval-1) {
  100.       for (i=worst_eval_p;i<interval;i++)
  101.         if (book_learn_eval[i] > best_after_worst_eval)
  102.           best_after_worst_eval=book_learn_eval[i];
  103.     }
  104.     else best_after_worst_eval=book_learn_eval[interval-1];
  105.  
  106. #if defined(DEBUG)
  107.     Print(128,"Learning analysis ...\n");
  108.     Print(128,"worst=%d  best=%d  baw=%d  wab=%d\n",
  109.           worst_eval, best_eval, best_after_worst_eval, worst_after_best_eval);
  110.     for (i=0;i<interval;i++)
  111.       Print(128,"%d(%d) ",book_learn_eval[i],book_learn_depth[i]);
  112.     Print(128,"\n");
  113. #endif
  114.  
  115. /*
  116.  ----------------------------------------------------------
  117. |                                                          |
  118. |   we now have the best eval for the first N moves out    |
  119. |   of book, the worst eval for the first N moves out of   |
  120. |   book, and the worst eval that follows the best eval.   |
  121. |   this will be used to recognize the following cases of  |
  122. |   results that follow a book move:                       |
  123. |                                                          |
  124.  ----------------------------------------------------------
  125. */
  126. /*
  127.  ----------------------------------------------------------
  128. |                                                          |
  129. |   (1) the best score is very good, and it doesn't drop   |
  130. |   after following the game further.  this case detects   |
  131. |   those moves in book that are "good" and should be      |
  132. |   played whenever possible.                              |
  133. |                                                          |
  134.  ----------------------------------------------------------
  135. */
  136.     if (best_eval == best_after_worst_eval) {
  137.       learn_value=best_eval;
  138.       for (i=0;i<interval;i++)
  139.         if (learn_value == book_learn_eval[i])
  140.           search_depth=Max(search_depth,book_learn_depth[i]);
  141.     }
  142. /*
  143.  ----------------------------------------------------------
  144. |                                                          |
  145. |   (2) the worst score is bad, and doesn't improve any    |
  146. |   after the worst point, indicating that the book move   |
  147. |   chosen was "bad" and should be avoided in the future.  |
  148. |                                                          |
  149.  ----------------------------------------------------------
  150. */
  151.     else if (worst_eval == worst_after_best_eval) {
  152.       learn_value=worst_eval;
  153.       for (i=0;i<interval;i++)
  154.         if (learn_value == book_learn_eval[i])
  155.           search_depth=Max(search_depth,book_learn_depth[i]);
  156.     }
  157. /*
  158.  ----------------------------------------------------------
  159. |                                                          |
  160. |   (3) things seem even out of book and remain that way   |
  161. |   for N moves.  we will just average the 10 scores and   |
  162. |   use that as an approximation.                          |
  163. |                                                          |
  164.  ----------------------------------------------------------
  165. */
  166.     else {
  167.       learn_value=0;
  168.       search_depth=0;
  169.       for (i=0;i<interval;i++) {
  170.         learn_value+=book_learn_eval[i];
  171.         search_depth+=book_learn_depth[i];
  172.       }
  173.       learn_value/=interval;
  174.       search_depth/=interval;
  175.     }
  176.     if (lv) learn_value=search_value;
  177.     if (lv == 0)
  178.       learn_value=LearnFunction(learn_value,search_depth,
  179.                                 crafty_rating-opponent_rating,
  180.                                 learn_value<0);
  181.     else learn_value=search_value;
  182. /*
  183.  ----------------------------------------------------------
  184. |                                                          |
  185. |   first, we are going to find every book move in the     |
  186. |   game, and note how many alternatives there were at     |
  187. |   every book move.                                       |
  188. |                                                          |
  189.  ----------------------------------------------------------
  190. */
  191.     save_wtm=wtm;
  192.     InitializeChessBoard(&tree->position[0]);
  193.     for (i=0;i<512;i++) n_book_moves[i]=0;
  194.     wtm=1;
  195.     for (i=0;i<512;i++) {
  196.       int *mv, cluster, key, test